package dao

import (
	"errors"
	"fmt"
	"net/url"
	"time"
)

//外链
type LinkData struct {
	Id          int64  `json:"id"`           //ID
	Title       string `json:"title"`        //名称
	Link        string `json:"link"`         //链接地址
	WebType     int    `json:"web_type"`     //网址类型
	ImgSrc      string `json:"img_src"`      //图片地址
	Comments    string `json:"comments"`     //网站描述
	ContactName string `json:"contact_name"` //联系人
	ContactMail string `json:"contact_mail"` //联系人Email
	Orders      int    `json:"orders"`       //排序
	IsShow      int    `json:"is_show"`      //IsShow
	AddTime     string `json:"add_time"`     //添加时间
	UpdateTime  string `json:"update_time"`  //更新时间
}

type Link LinkData

//Init
func (l *Link) Init() *Link {

	return l
}

//添加
func (l *Link) Add() (err error) {
	Result, err := Db.Exec("insert into `bg_link` set title=?,link=?,web_type=?,img_src=?,comments=?,contact_name=?,contact_mail=?,orders=?,is_show=?,add_time=?",
		l.Title, l.Link, l.WebType, l.ImgSrc, l.Comments, l.ContactName, l.ContactMail, l.Orders, l.IsShow, l.AddTime)
	if err != nil {
		return
	}
	id, err := Result.LastInsertId()
	if err != nil {
		return
	}
	l.Id = id
	return nil
}

//Delete
func (l *Link) Delete() (err error) {
	return
}

//update
func (l *Link) Update() (err error) {
	return
}

//列表
func (l Link) ListAll(limitNum uint, isShow int) (links []Link, err error) {

	whereStr := ""
	if isShow == 1 {
		whereStr = "is_show=1"
	} else if isShow == 0 {
		whereStr = "is_show=0"
	} else {
		whereStr = "1=1"
	}

	sql := "select `id`,`title`,`link`,`web_type`, IFNULL(`img_src`,''),IFNULL(`comments`,''),IFNULL(`contact_name`,''),IFNULL(`contact_mail`,''), `orders`,`is_show`,IFNULL(add_time,''),IFNULL(update_time,'') from bg_link where " + whereStr + " order by orders asc "
	if limitNum > 0 {
		sql += fmt.Sprintf(" limit %d", limitNum)
	}
	if Db == nil {
		InitMysql()
	}

	rows, err := Db.Query(sql)
	if err != nil {
		return
	}
	defer rows.Close()

	var lk Link
	var err1 error
	for rows.Next() {
		err1 = rows.Scan(&lk.Id, &lk.Title, &lk.Link, &lk.WebType, &lk.ImgSrc, &lk.Comments, &lk.ContactName, &lk.ContactMail, &lk.Orders, &lk.IsShow, &lk.AddTime, &lk.UpdateTime)
		if err1 != nil {
			fmt.Println(err1.Error())
		}
		links = append(links, lk)
	}

	return
}

//check title
func (l *Link) CheckFormTitle(title string) error {
	if title == "" {
		return errors.New("网站名称为空")
	}
	if len(title) < 3 || len(title) > 20 {
		return errors.New("网站名称太短或太长")
	}
	l.Title = title
	return nil
}

//check link
func (l *Link) CheckFormLink(link string) error {
	if link == "" {
		return errors.New("网站地址为空")
	}
	l.Link = link
	return nil
}

//check comment
func (l *Link) CheckFormComment(comments string) error {
	if comments == "" {
		return nil
	}
	if len(comments) > 200 {
		return errors.New("网站描述太长")
	}
	l.Comments = comments
	return nil
}

//check userName
func (l *Link) CheckFormContactName(name string) error {
	if name == "" {
		return nil
	}
	if len(name) > 12 {
		return errors.New("网站联系人姓名太长")
	}
	l.ContactName = name
	return nil
}

//check email
func (l *Link) CheckFormContactEmail(email string) error {
	if email == "" {
		return nil
	}
	if len(email) > 18 {
		return errors.New("网站联系人Email太长")
	}
	l.ContactMail = email
	return nil
}

func (l *Link) CheckForm(post url.Values) (err error) {
	if err = l.CheckFormTitle(post.Get("title")); err != nil {
		return
	}
	if err = l.CheckFormLink(post.Get("link")); err != nil {
		return
	}
	if err = l.CheckFormComment(post.Get("comments")); err != nil {
		return
	}
	if err = l.CheckFormContactName(post.Get("contact_name")); err != nil {
		return
	}
	if err = l.CheckFormContactEmail(post.Get("contact_mail")); err != nil {
		return
	}

	if l.WebType == 0 {
		l.WebType = 22
	}
	l.ImgSrc = ""
	if l.Orders == 0 {
		l.Orders = 100
	}
	if l.IsShow != 1 {
		l.IsShow = 0
	}
	if l.AddTime == "" {
		l.AddTime = time.Now().Format(TimeLayout)
	}
	return nil
}

/**
 * 获取链接列表
 * @param limitNum 获取的数量 0表示所有
 */
func GetLinks(limitNum uint) (links []Link, err error) {
	var lk Link
	links, err = lk.ListAll(limitNum, 1)
	return
}
